package com.itheima.service.impl;

import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.EncryptUtil;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会员服务
 *
 * @create 2020/11/27 0027 10:29
 */
@Service
@Transactional
public class MemberServiceImpl implements MemberService {
    
    @Autowired
    private MemberDao memberDao;
    
    @Override
    public Member findByTelephone(String telephone) {
        return memberDao.findByTelephone(telephone);
    }
    
    //保存会员信息
    @Override
    public void add(Member member) {
        String password = member.getPassword();
        if (password != null) {
            //使用md5将明文密码进行加密
            EncryptUtil encryptUtil = EncryptUtil.getInstance();
            password = encryptUtil.MD5(password);
            member.setPassword(password);
        }
        memberDao.add(member);
    }

    @Override
    public List<Integer> findMemberCountByMonths(List<String> months) {
        List<Integer> memberCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";
            Integer count = memberDao.findMemberCountBeforeDate(date);
            memberCount.add(count);
        }
        return memberCount;
    }

    @Override
    public List<Map<String, Object>> getMemberCount() {
        return memberDao.getMemberCount();
    }

    @Override
    public List<Map<String, Object>> getAgeCount() {
        return memberDao.getAgeCount();
    }
}
